﻿using LanqiQAwebapi.Dto;
using LanqiQAwebapi.Interface;
using LanqiQAwebapi.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;

namespace LanqiQAwebapi.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    [EnableCors("AllowCors")]
    public class EssayController : Controller
    {
        private readonly ILogger<EssayController> _logger;
        private readonly IUserServer _userServer;
        private readonly Iessay _essay;
        public EssayController(ILogger<EssayController> logger, IUserServer userServer, Iessay essay)
        {
            _logger = logger;
            _userServer = userServer;
            _essay = essay;
        }

        [EnableCors("AllowCors")]
        [HttpPost("TestTaken")]
        [Authorize(Roles = "User")]

        public JsonResult TestTaken(string test)
        {
            JsonResult s = new JsonResult(test);
            return s;
        }
        ///用户提问
        /// [EnableCors("AllowCors")]
        [HttpPost("AddQuestions")]
        // [Authorize(Roles = "User")]

        public async Task<BaseResponse> QuestionsAdd(AddIssuesDto AddIssuesDto)
        {
            var response = new BaseResponse();
            response.Code = 400;
            response.Success = false;
            response.Message = "添加失败";

            var data = await _essay.AddQuestionsAsync(AddIssuesDto);
            if (data)
            {
                response.Code = 200;
                response.Success = data;
                response.Message = "添加成功";
                return response;
            }
            return response;
        }
        /// <summary>
        /// 获取热门问题
        /// </summary>
        /// <returns></returns>
        [HttpPost("GetHotQuestions")]
        // [Authorize(Roles = "User")]
        public BaseResponse<List<HotQa>> HotQuestions()
        {
            var data = _essay.GetHotQuestion();
            return BaseResponse<List<HotQa>>.SetSuccess(data);

        }
        /// <summary>
        /// 通过问题查询回答
        /// </summary>
        /// <param name="questions"></param>
        /// <returns></returns>
        [HttpPost("GetQuestions")]
        // [Authorize(Roles = "User")]

        public BaseResponse<List<Qa>> Questions(string questions)
        {
            var data = _essay.GetQuestion(questions);
            return BaseResponse<List<Qa>>.SetSuccess(data);

        }


        [HttpPost("GetQuestionsById")]
        // [Authorize(Roles = "User")]

        public BaseResponse<List<Qa>> QuestionsById(GetquestionByidDto id)
        {
            var data = _essay.GetQuestionById(id.Id);
            return BaseResponse<List<Qa>>.SetSuccess(data);

        }
        [HttpPost("AnswerAdd")]
        // [Authorize(Roles = "User")]
        public BaseResponse<bool> addAnswer(AddAnswerDto addAnswerDto)
        {
            var data = _essay.AddAnswer(addAnswerDto);
            return BaseResponse<bool>.SetSuccess(data);

        }
        [HttpPost("AnswerShowByUserid")]
        public BaseResponse<List<Qa>> ShowAnswerByNameAndUserID(ShowAnswerDto showAnswerDto)
        {
            var data = _essay.ShowAnswer(showAnswerDto);

            return BaseResponse<List<Qa>>.SetSuccess(data);

        }
        /// <summary>
        /// 关注的人的回答
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [HttpPost("ShowFollowAnswers")]
        public BaseResponse<List<FollowedUserAnswerDto>> ShowFollowAnswers(int userId)
        {

         return BaseResponse<List< FollowedUserAnswerDto> >.SetSuccess( _essay.ShowFollowAnswers(userId));


        }
    }
}
